package com.monkey.core.plugin.code.tpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.monkey.core.plugin.dto.MonkeyDto;
import com.monkey.utils.MonkeyUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project  : monkey
 * Author   : Wu Tian Qiang
 * Date     : 2016/10/10
 */
public class SimpleCodeTemplateGeneratorImpl extends AbstractCodeTemplateGenerator {

    //=======================生成Service===========================
    protected JSONObject prepareServiceParams(MonkeyDto monkeyDto) {
        try {
            JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
            modelParams.putAll(getCommonParams(monkeyDto));
            return modelParams;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected String getServiceImplTemplate() {
        return "templates/simple/serviceImpl.java.vm";
    }

    protected String getServiceTemplate() {
        return "templates/simple/service.java.vm";
    }

    //=======================生成View===========================

    protected JSONObject prepareViewParams(MonkeyDto monkeyDto) {
        try {
            JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
            modelParams.putAll(getCommonParams(monkeyDto));
            String className = modelParams.getString("className");
            modelParams.put("lowerClassName", MonkeyUtils.lowercaseFirstChar(className));

            JSONArray properties = JSON.parseArray(URLDecoder.decode(monkeyDto.getString("properties"), "UTF-8"));
            modelParams.put("properties", properties);

            modelParams.putAll(prepareCustomParams(monkeyDto));
            return modelParams;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 处理自定义模版部分的数据
     *
     * @param monkeyDto
     * @throws UnsupportedEncodingException
     */
    protected Map<String, Object> prepareCustomParams(MonkeyDto monkeyDto) throws UnsupportedEncodingException {
//        JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
        Map<String, Object> params = new HashMap<>();
        JSONArray properties = JSON.parseArray(URLDecoder.decode(monkeyDto.getString("properties"), "UTF-8"));
        JSONObject customValues = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("customValues"), "UTF-8"));
        params.put("title", customValues.getString("title"));

        String queryFields = customValues.getString("queryFields");
        StringBuilder sb = new StringBuilder("{");
        if (MonkeyUtils.isNotEmpty(queryFields)) {
            List<JSONObject> queryProperties = new ArrayList<>();
            String[] fields = queryFields.split(",");
            for (String field : fields) {
                for (Object o : properties) {
                    JSONObject pro = (JSONObject) o;
                    if (field.trim().equals(pro.getString("name").trim())) {
                        queryProperties.add(pro);
                        sb.append("'search." + pro.getString("name") + "_like':").append(pro.getString("name") + ',');
                    }
                }
            }
            params.put("queryProperties", queryProperties);
            params.put("query", true);
            if (sb.length() > 1) {
                sb.deleteCharAt(sb.length() - 1);
            }
            sb.append("}");
            params.put("params", sb);
        }
        return params;
    }

    @Override
    protected String getWindowControllerTemplate() {
        return "templates/simple/view/window.js.vm";
    }

    @Override
    protected String getViewModelControllerTemplate() {
        return "templates/simple/view/viewmodel.js.vm";
    }

    @Override
    protected String getModelControllerTemplate() {
        return "templates/simple/view/model.js.vm";
    }

    @Override
    protected String getStoreControllerTemplate() {
        return "templates/simple/view/store.js.vm";
    }

    protected String getViewTemplate() {
        return "templates/simple/view/view.js.vm";
    }

    @Override
    protected String getViewControllerTemplate() {
        return "templates/simple/view/controller.js.vm";
    }
//=======================生成Controller===========================

    protected String getControllerTemplate() {
        return "templates/simple/controller.java.vm";
    }

    protected JSONObject prepareControllerParams(MonkeyDto monkeyDto) {
        try {
            JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
            modelParams.putAll(getCommonParams(monkeyDto));
            String className = modelParams.getString("className");
            modelParams.put("lowerClassName", MonkeyUtils.lowercaseFirstChar(className));
            return modelParams;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //=======================生成Repository===========================

    protected JSONObject prepareRepositoryParams(MonkeyDto monkeyDto) {
        try {
            JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
            modelParams.putAll(getCommonParams(monkeyDto));
            return modelParams;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected String getRepositoryTemplate() {
        return "templates/simple/repository.java.vm";
    }

    //=======================生成Domain===========================

    protected String getDomainTemplate() {
        return "templates/simple/domain.java.vm";
    }

    /**
     * 准备生成Domain的数据
     *
     * @param monkeyDto
     * @return
     */
    protected Map<String, Object> prepareDomainParams(MonkeyDto monkeyDto) {
        try {
            JSONObject modelParams = JSON.parseObject(URLDecoder.decode(monkeyDto.getString("modelParam"), "UTF-8"));
            JSONArray properties = JSON.parseArray(URLDecoder.decode(monkeyDto.getString("properties"), "UTF-8"));

            modelParams.putAll(getCommonParams(monkeyDto));
            Map<String, Object> params = new HashMap<>();
            addPropertiesFistUppercaseChar(properties);

//            modelParams.put("parseInterface", getParseInterface(modelParams));
            params.put("modelParams", modelParams);
            params.put("properties", properties);
            return params;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void addPropertiesFistUppercaseChar(JSONArray properties) {
        for (Object o : properties) {
            JSONObject property = (JSONObject) o;
            property.put("upname", MonkeyUtils.uppercaseFirstChar(property.getString("name")));
        }
    }


}
